class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> hash1(26, 0);
        vector<int> hash2(26, 0);
        vector<int> ret;

        for (auto ch : p)
            hash2[ch - 'a']++;

        for (int left = 0, right = 0, count = 0; right < s.size(); right++)
        {
            if (++hash1[s[right] - 'a'] <= hash2[s[right] - 'a'])    count++;
            if (right - left + 1 > p.size())
            {
                int out = s[left++];
                if (hash1[out - 'a']-- <= hash2[out - 'a'])  count--;
            }
            if (count == p.size())
            {
                ret.push_back(left);
            }
        }
        return ret;
    }
};